package com.xing.service.user;

import com.xing.dao.BaseDao;
import com.xing.dao.user.UserDao;
import com.xing.dao.user.UserDaoImpl;
import com.xing.pojo.User;
import org.junit.Test;

import java.sql.SQLException;
import java.util.List;

public class UserServiceImpl implements UserService{

    //业务层都会调用dao层，所以我们要引入dao层
    private UserDao userDao;
    public UserServiceImpl(){
        userDao=new UserDaoImpl();
    }
    @Override
    public User login(String useCode, String pwd) throws SQLException, ClassNotFoundException {
        User user = null;
        BaseDao baseDao = new BaseDao();
        //通过业务层调用对应的具体的数据库操作
        user = userDao.getLoginUser(useCode,pwd);
        baseDao.closeAll();
        return user;
    }

    @Override
    public boolean updatePwd(int id, String pwd) throws SQLException, ClassNotFoundException {
        Boolean flag = false;
        BaseDao baseDao = new BaseDao();
        if (userDao.updatePwd(id,pwd)>0){
            flag= true;
            baseDao.closeAll();
        }
        return flag;
    }

    @Override
    //查询记录数
    public int getUserCount(String username, int userRole) throws SQLException, ClassNotFoundException {
        int userCount = 0;
        BaseDao baseDao = new BaseDao();
        userCount = userDao.getUserCount(username, userRole);
        baseDao.closeAll();
        return userCount;
    }

    @Override
    public List<User> getUserList(String queryUsername, int queryUserRole, int currentPageNo, int pageSize) throws SQLException, ClassNotFoundException {
        List<User> userList = null;
        System.out.println("username"+queryUsername);
        System.out.println("userRole"+queryUserRole);
        System.out.println("currentPageNo"+currentPageNo);
        System.out.println("pageSize"+pageSize);
        BaseDao baseDao = new BaseDao();
        userList = userDao.getUserList(queryUsername, queryUserRole, currentPageNo, pageSize);
        baseDao.closeAll();
        return userList;
    }

    @Override
    public User getUserByUserCode(String userCode) throws SQLException, ClassNotFoundException {
        User user = null;
        BaseDao baseDao = new BaseDao();
        user = userDao.getUserByUserCode(userCode);
        baseDao.closeAll();
        return user;
    }

    @Override
    public User getUserById(int id) throws SQLException, ClassNotFoundException {
        User user = null;
        BaseDao baseDao = new BaseDao();
        user = userDao.getUserById(id);
        baseDao.closeAll();
        return user;
    }

    @Override
    public int addUser(User user) throws SQLException, ClassNotFoundException {
        int result = 0;
        BaseDao baseDao = new BaseDao();
        result = userDao.addUser(user);
        baseDao.closeAll();
        return  result;
    }

    @Override
    public int delUser(int id) throws SQLException, ClassNotFoundException {
        int result = 0;
        BaseDao baseDao = new BaseDao();
        result = userDao.delUser(id);
        baseDao.closeAll();
        return  result;
    }

    @Override
    public int modifyUser(User user) throws SQLException, ClassNotFoundException {
        int result = 0;
        BaseDao baseDao = new BaseDao();
        result = userDao.modifyUser(user);
        baseDao.closeAll();
        return  result;
    }

    @Test
    public void test() throws SQLException, ClassNotFoundException {
        UserServiceImpl userService = new UserServiceImpl();

    }

}
